package com.devunion.bogdan.view.project.dialogs;

import com.devunion.bogdan.model.project.Project;
import com.devunion.bogdan.view.util.i18n;
import com.devunion.bogdan.view.util.DialogHelper;
import com.devunion.bogdan.view.util.ComponentFactory;

import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author Viktar Liaskovich
 */
public class ManageProjectsDialog extends JDialog {
    private JList jListProjects;
    private JButton jButtonDeleteProject;

    public ManageProjectsDialog(Frame owner) {
        super(owner, i18n.getString("ManageProjectsDialog.Title"), true);

        getContentPane().setLayout(new GridBagLayout());

        initComponents();

        pack();
        setResizable(false);
        setLocationRelativeTo(null);
    }

    private void initComponents() {

        DialogHelper.placeComponent(getContentPane(), new JLabel(i18n.getString("ManageProjectsDialog.ProjectsList")),
                0, 0, 1, 1,
                0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                15, 10, 10, 10);

        initListOfProjects();
        DialogHelper.placeComponent(getContentPane(), new JScrollPane(jListProjects),
                0, 1, 1, 1,
                1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
                15, 10, 10, 10);

        DialogHelper.placeComponent(getContentPane(), createButtonsPanel(),
                1, 0, 1, 2,
                1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
                0, 10, 10, 0);
    }

    private Component createButtonsPanel() {
        JPanel result = new JPanel(new GridLayout(2, 1, 5, 5));

        jButtonDeleteProject = new JButton(i18n.getString("ManageProjectsDialog.Delete"));
        jButtonDeleteProject.setEnabled(false);

        jButtonDeleteProject.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object[] selectedProjects = jListProjects.getSelectedValues();

                for (Object project : selectedProjects) {
                    ((DefaultListModel) jListProjects.getModel()).removeElement(project);

                    // TODO: delete from base
                    System.out.println("Delete project: " + project);
                }
            }
        });

        result.add(jButtonDeleteProject);

        result.add(ComponentFactory.createPanelButtonOkCancel(this, ComponentFactory.OkCancelPanel.Layout.VERTICAL, new ComponentFactory.ActionHandler() {
            public void execute() {
                // TODO: remove projects from database.
                setVisible(false);
            }
        }, new ComponentFactory.ActionValidator() {
            public boolean validate() {
                return true;
            }
        }));

        return result;
    }

    public void initListOfProjects() {
        List<Project> projects = new ArrayList();

        Vector model = new Vector(projects);


        jListProjects = new JList(model);
        jListProjects.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                boolean isAnySelected = jListProjects.getSelectedValues().length != 0;
                jButtonDeleteProject.setEnabled(isAnySelected);
            }
        });
    }

}